﻿Public Class xtf_ABMvehiculo

    Dim ulg_row As DataRow
    Dim obj_vehiculo As cls_vehiculo_ = Nothing

    Public Sub New()
        InitializeComponent()
        AgregarValidaciones()
        Cargarpersonal_()
        Cargarcentrocostoorden_()
        Cargarmarca_()
        Cargarvehiculotipo_()
        Cargarcolor_()
    End Sub

    Private Sub Cargarvehiculotipo_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_vehiculotipo.EditValue
        Dim vehiculotipo_ As New cls_vehiculotipo_
        vehiculotipo_.CargarTodos()
        cargarCombo(cbo_vehiculotipo, vehiculotipo_.Items)
        cbo_vehiculotipo.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_vehiculotipo.EditValue)
    End Sub

    Private Sub Cargarcolor_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_color.EditValue
        Dim color_ As New cls_color_
        color_.CargarTodos()
        cargarCombo(cbo_color, color_.Items)
        cbo_color.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_color.EditValue)
    End Sub

    Private Sub Cargarpersonal_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_personal.EditValue
        Dim personal_ As New cls_personal_
        personal_.CargarTodos()
        cargarCombo(cbo_personal, personal_.Items)
        cbo_personal.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_personal.EditValue)
    End Sub

    Private Sub Cargarcentrocostoorden_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_centrocostoorden.EditValue
        Dim centrocostoorden_ As New cls_centrocostoorden_
        centrocostoorden_.CargarTodos()
        cargarCombo(cbo_centrocostoorden, centrocostoorden_.Items)
        cbo_centrocostoorden.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_centrocostoorden.EditValue)
    End Sub

    Private Sub Cargarmarca_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_marca.EditValue
        Dim marca_ As New cls_marca_
        marca_.CargarTodos()
        cargarCombo(cbo_marca, marca_.Items)
        cbo_marca.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_marca.EditValue)
    End Sub

    Private Sub Cargarmodelo_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_modelo.EditValue
        Dim modelo_ As New cls_modelo_
        modelo_.CargarXidmarca(cbo_marca.EditValue)
        cargarCombo(cbo_modelo, modelo_.Items)
        cbo_modelo.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_modelo.EditValue)
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_vehiculo = New cls_vehiculo_
            btn_Registro.Enabled = False
            txt_año.Text = Now.Year
            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef vehiculo As cls_vehiculo_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_vehiculo) Then
            obj_vehiculo = New cls_vehiculo_
        End If
        obj_vehiculo = vehiculo
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_vehiculo.ToString
        txt_ID.Text = obj_vehiculo.idvehiculo

        cbo_modelo.EditValue = obj_vehiculo.idmodelo
        cbo_marca.EditValue = obj_vehiculo.idmarca
        txt_año.Text = New Date(obj_vehiculo.año, 1, 1)
        txt_patente.Text = obj_vehiculo.patente
        cbo_personal.EditValue = obj_vehiculo.idpersonal
        cbo_centrocostoorden.EditValue = obj_vehiculo.idcentrocostoorden
        cbo_color.EditValue = obj_vehiculo.idcolor
        txt_inventario.Text = obj_vehiculo.inventario
        txt_denominacion.Text = obj_vehiculo.denominacion
        cbo_vehiculotipo.EditValue = obj_vehiculo.idvehiculotipo

        btn_Registro.Enabled = True

        vehiculo.marca = Nothing
        vehiculo.color = Nothing
        vehiculo.personal = Nothing
        vehiculo.marca = Nothing
        vehiculo.modelo = Nothing
        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If

        'REGISTROS DUPLICADOS----------------------------------------------------
        Dim col As New Collections.Generic.List(Of stc_campoValor)
        Dim comp1 As New stc_campoValor
        comp1.campo = "patente"
        comp1.valor = txt_patente.Text
        col.Add(comp1)

        If ComprobarDuplicado("vehiculo_", col, "idvehiculo <> " & IIf(txt_ID.Text = "", 0, txt_ID.Text)) Then
            ErrManagerShow("000010", Me, " Dato: Dominio")
            Return False
        End If
        'REGISTROS DUPLICADOS----------------------------------------------------

   
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_vehiculo.idmodelo = cbo_modelo.EditValue
            obj_vehiculo.idmarca = cbo_marca.EditValue
            obj_vehiculo.año = txt_año.Text
            obj_vehiculo.patente = txt_patente.Text
            obj_vehiculo.idpersonal = cbo_personal.EditValue
            obj_vehiculo.idcentrocostoorden = cbo_centrocostoorden.EditValue
            obj_vehiculo.inventario = txt_inventario.Text
            obj_vehiculo.denominacion = txt_denominacion.Text
            obj_vehiculo.idvehiculotipo = cbo_vehiculotipo.EditValue
            obj_vehiculo.idcolor = cbo_color.EditValue

            If txt_ID.Text = "" Then
                obj_vehiculo.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOvehiculo_).AgregarItem(obj_vehiculo)
                End If
                Modificar(obj_vehiculo, frm_listado, ulg_row)
            Else
                obj_vehiculo.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOvehiculo_).ModificarItem(obj_vehiculo, ulg_row)
                End If
                Modificar(obj_vehiculo, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_vehiculo.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_vehiculo.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub cbo_marca_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_marca.EditValueChanged
        Cargarmodelo_()
    End Sub
End Class

